package per.hnvcam.fuzzy.impl;

import per.hnvcam.fuzzy.MembershipFunction;

/**
 * Created by IntelliJ IDEA.
 * User: Vi Cam
 * Date: 04/02/2012
 * Time: 21:37
 * To change this template use File | Settings | File Templates.
 */
public class TrapezoidalMembershipFunctionImpl implements MembershipFunction {

   private double [] params;

   @Override
   public void setParameters(double[] params) {
      if (params.length != 4) {
         throw new IllegalArgumentException("Four parameters are expected.");
      }
      this.params = params;
   }

   @Override
   public double getValueOf(double x) {
      if (this.params == null) {
         throw new NullPointerException("The parameters must be set.");
      }
      return Math.max(Math.min(Math.min((x - params[0])/(params[1] - params[0]),
            (params[3] - x)/(params[3] - params[2])), 1),
            0);
   }
}
